package org.example.authservice.pojo;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author 浦慧慧
 * @since 2024/12/15 11.34
 * @version 1.0
 */
public class MakeMenuTree {
    public static List<Permission> makeTree(List<Permission> permissionList, Integer id){
        List<Permission> list = new ArrayList<>();
        Optional.ofNullable(permissionList).orElse(new ArrayList<>())
                .stream()
                .filter(item -> item!=null && item.getParentId().equals(id))
                .forEach(item -> {
                    Permission permission= new Permission();
                    BeanUtils.copyProperties(item,permission);
                    permission.setLabel(item.getComment());
                    permission.setValue(item.getId());
                    //查找下级：递归循环
                    List<Permission> children = makeTree(permissionList, item.getId());
                    permission.setChildren(children);
                    list.add(permission);
                });
        return list;
    }
}
